<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        offset="50"
        :immediate-check = false
        @load="onLoad"
      >
      <!-- 文章列表 -->
      <article-item
      v-for="(item,index) in articleList"
      :key="item.art_id + index"
      :obj = item
      ></article-item>
      </van-list>
    </van-pull-refresh>

  </div>
</template>

<script>
import ArticleItem from './components/ArticleItem.vue'
import { articlesListAPI } from '@/api'
import { timeAgo } from '@/utils/date.js'
export default {
  components: {
    ArticleItem
  },
  props: {
    cid: Number
  },
  async created () {
    this.getArticleList()
  },
  data () {
    return {
      articleList: [],
      finished: false,
      loading: false,
      nextTime: null,
      isLoading: false
    }
  },
  methods: {
    async getArticleList () {
      const res = await articlesListAPI({
        // channelId随用户点击切换
        channelId: this.cid,
        timestamp: this.nextTime
      })
      res.data.data.results.forEach(item => {
        item.pubdate = timeAgo(item.pubdate)
      })
      // this.articleList = res.data.data.results
      if (res.data.data.results.length === 0) {
        this.finished = true
      } else {
        this.nextTime = res.data.data.pre_timestamp
        this.articleList = [...this.articleList, ...res.data.data.results]
        this.loading = false
      }
    },
    onLoad () {
      // console.log('页面滚动了')
      if (this.articleList.length > 0) {
        this.getArticleList()
      }
    },
    // 上拉释放
    onRefresh () {
      this.nextTime = null
      this.articleList = []
      this.getArticleList()
      this.isLoading = false
    }
  }
}
</script>
